package com.example.demo.jucConcurrent.ThreadSynchronized;

public class SynchronizedObjectLock implements  Runnable {

   static SynchronizedObjectLock instence =  new SynchronizedObjectLock();

    @Override
    public void run() {
        //同步代码块形成一个锁为this,两个线程使用的锁时一样的,线程1必须要等到线程0释放了该锁之后,才能执行
        synchronized (this){
            System.out.println("我是主线程"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"结束");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instence);
        Thread t2 = new Thread(instence);
        t1.start();
        t2.start();
    }
}
